package com.nuoniu.sibanyun.service.crm.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.nuoniu.sibanyun.common.result.HttpResult;
import com.nuoniu.sibanyun.entity.crm.CrmUserField;
import com.nuoniu.sibanyun.mapper.crm.CrmUserFieldMapper;
import com.nuoniu.sibanyun.service.crm.ICrmUserFieldService;
import com.nuoniu.sibanyun.userThread.UserInfo;
import com.nuoniu.sibanyun.userThread.UserThreadLocal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * <p>
 * 用户显示字段策略 服务实现类
 * </p>
 *
 * @author baiqiang
 * @since 2020-06-23
 */
@Service
public class CrmUserFieldServiceImpl extends ServiceImpl<CrmUserFieldMapper, CrmUserField> implements ICrmUserFieldService {

    @Autowired
    private CrmUserFieldMapper fieldMapper;


    @Override
    public void saveCrmFiled(CrmUserField field) {
        LocalDateTime now = LocalDateTime.now();
        UserInfo userInfo = UserThreadLocal.get();
        field.setCreateUserId(userInfo.getUserId());
        CrmUserField userField = fieldMapper.getFiled(field);
        field.setUpdateUserId(userInfo.getUserId());
        field.setUpdateDate(now);
        if(userField != null) {
            field.setId(userField.getId());
            updateById(field);
        }else {
            field.setCreateUserId(userInfo.getUserId());
            field.setCreateDate(now);
            save(field);
        }
    }

    @Override
    public CrmUserField detail(CrmUserField field) {
        UserInfo userInfo = UserThreadLocal.get();
        field.setCreateUserId(userInfo.getUserId());
        return fieldMapper.getFiled(field);
    }

    @Override
    public int concatUserField(Integer companyId,String type, String fieldCode) {
        return baseMapper.concatUserField(companyId, type,fieldCode);
    }
}
